package com.ciwong.epaper.modules.epaper.ui.newlistenspeak;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.widget.Button;
import android.widget.Toast;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.LsRedoAnswersEvent;
import com.ciwong.epaper.modules.cordva.ActivityPlugin;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.cordva.SubmitEvent;
import com.ciwong.epaper.modules.dbbean.AnswerContent;
import com.ciwong.epaper.modules.dbbean.LswAnswer;
import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.epaper.bean.ListenspeakExam;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.epaper.bean.ModuleContent;
import com.ciwong.epaper.modules.epaper.bean.Questions;
import com.ciwong.epaper.modules.epaper.bean.ResourceDetail;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.epaper.ui.newlistenspeak.bean.LswAnswerContent;
import com.ciwong.epaper.modules.epaper.ui.newlistenspeak.bean.LswAnswerSubContent;
import com.ciwong.epaper.modules.epaper.ui.newlistenspeak.bean.LswRedoSubAnswer;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.epaper.util.ListenSpeakUtil;
import com.ciwong.epaper.modules.me.bean.WorkContents;
import com.ciwong.epaper.modules.me.ui.BuyServiceActivity;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.ESystem;
import com.ciwong.epaper.util.FeedbackUtil;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.epaper.util.SerializableManager;
import com.ciwong.epaper.util.download.DownLoadInfo;
import com.ciwong.epaper.widget.NewLoginDialog;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.mobilelib.bean.Main;
import com.ciwong.mobilelib.i.GoBackListener;
import com.ciwong.mobilelib.ui.BrowserActivity;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.ciwong.mobilelib.widget.CWDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.cordova.engine.SystemWebViewClient;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/5/25.
 */
public class LswRedoActivity extends BaseHtmlActicity {

    private final String TAG = "redoques";
    private DownLoadInfo downLoadInfo;
    private Module mModule;
    private int position;
    private String mWorkId = "0";
    private String mClassId;
    protected int mServiceId;
    protected int lsMode;
    //截止时间
    private long effectivDate = -1;
    //选题信息
    protected String checkedResource;
    private ListenspeakExam mListenspeakExam;
    private WorkContents mWorkContents;
    private int workTime;
    private Context mContext;
    protected ArrayList<LswAnswer> mLswAnswerList = new ArrayList<LswAnswer>();
    private String mCallBackId;
    private boolean isCorrect;
    private String uuid;
    private MyThread myThread;
    protected Handler mHandler = new Handler();

    protected ArrayList<ResourceDetail> isDoRecords;// 是否做过的记录

    @Override
    protected void preCreate() {
        super.preCreate();

        downLoadInfo = (DownLoadInfo) getIntent().getSerializableExtra(IntentFlag.INTENT_FLAG_DOWNLOAD_INFO);
        mModule = (Module) getIntent().getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ);
        position = getIntent().getIntExtra(IntentFlag.INTENT_FLAG_POSITION, 0);
        String workId = getIntent().getStringExtra(IntentFlag.INTENT_FLAG_WORK_ID);

        if (!TextUtils.isEmpty(workId)) {
            mWorkId = workId;
        }

        mClassId = getIntent().getStringExtra(IntentFlag.INTENT_FLAG_CLASS_ID);
        mServiceId = getIntent().getIntExtra(IntentFlag.INTENT_FLAG_SERVICE_ID, -1);
        workTime = getIntent().getIntExtra(IntentFlag.INTENT_FLAG_WORK_TIME, 1);
        checkedResource = getIntent().getStringExtra(IntentFlag.INTENT_FLAG_CHECKED_RESOURCE);
        mLswAnswerList = (ArrayList<LswAnswer>) getIntent().getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ_LIST);
        mListenspeakExam = (ListenspeakExam) getIntent().getSerializableExtra(IntentFlag.INTENT_FLAG_LSW_EXAM);
        mWorkContents = (WorkContents) getIntent().getSerializableExtra(IntentFlag.INTENT_FLAG_LSW_WORK_CONTENTS);
        lsMode = getIntent().getIntExtra(IntentFlag.INTENT_FLAG_ACTION, ListenSpeakUtil.LsMode.OWN_PRACTISE);
        doWorkType = getIntent().getIntExtra(IntentFlag.INTENT_FLAG_DO_WORK_TYPE, 0);
        isDoRecords = (ArrayList<ResourceDetail>) getIntent().getSerializableExtra(IntentFlag.INTENT_FLAG_LSW_IS_DO_REDORD);
        effectivDate = getIntent().getLongExtra(IntentFlag.INTENT_FLAG_WORK_EFFECTIV_TIME, -1);
        isCorrect = getIntent().getBooleanExtra(IntentFlag.INTENT_FLAG_CORRECT, false);


        if (null != mLswAnswerList) {
            if (mLswAnswerList.size() > 0) {
                uuid = mLswAnswerList.get(0).getWorkSaveUUid();
            }
            Log.d(TAG, "########mLswAnswerList.size()##############" + mLswAnswerList.size());
            Log.d(TAG, "########workTime##############" + workTime);
        }

    }

    @Override
    public void loadData() {
        EventBus.getDefault().register(this);
        //网络没有仍然能加载出来
        //super.loadData();
        Intent intent = getIntent();
        mContext = this;
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                webView.setWebContentsDebuggingEnabled(true);
            }
        } catch (Exception e) {

        }


        if (null != intent) {
            setTitleText(intent.getStringExtra(BrowserActivity.IntentString.INTENT_STRING_TITLE));
        }
        int examMode = 0;
        if (mWorkContents != null && doWorkType != 1) {
            //考试模式，不容许重做
            examMode = mWorkContents.getExamMode();
        }
        setStartURL("file://" + ESystem.getLsReDoPath() + File.separator + "lsReform.html?examMode=" + examMode + "&doWorkType=" + doWorkType);
        //setStartURL("http://192.168.18.181/mobile/v1706_moliban/lsReform/index.html?");

        loadUrl(startURL.toString());
        if (isCorrect) {
            ((Button) findViewById(R.id.btn_commit_homework)).setText(R.string.correct_question);
        } else {
            ((Button) findViewById(R.id.btn_commit_homework)).setText(R.string.comit_homework);
        }

        if (null != mWorkContents && mWorkContents.getExamMode() == 2) {
            //考试模式自动提交作业
            myThread = new MyThread();
            mHandler.postDelayed(myThread, 3000);
        }else {
            findViewById(R.id.btn_commit_homework).setVisibility(View.VISIBLE);
            findViewById(R.id.btn_commit_homework).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isCorrect) {
                        CWSys.setSharedString(ListenSpeakUtil.getUuidSaveKey(downLoadInfo, mModule, position, mWorkId, doWorkType), null);
                    }
                    if (EConstants.IS_YOUKE) {
                        EConstants.LISTENSPEAK_IS_YOUKE_LOGIN = true;
                        NewLoginDialog newLoginDialog = new NewLoginDialog(LswRedoActivity.this);
                        newLoginDialog.showLoginDialog();
                    } else {
                        int noOverCount = 0;// 未做数

                        if (null != isDoRecords && isDoRecords.size() > 0) {
                            for (ResourceDetail isDone : isDoRecords) {
                                if (!isDone.isDo()) {
                                    noOverCount++;
                                }
                            }
                        }

                        for (LswAnswer lswAnswer : mLswAnswerList) {

                            if (lswAnswer.getAnswerType() == Questions.AnswerType.QUESTION_TYPE_FILL_BLANK || lswAnswer.getAnswerType() == Questions.AnswerType.QUESTION_TYPE_CHOICE) {
                                for (AnswerContent answerContent : lswAnswer.getAnswerContents()) {
                                    if (answerContent.getContent().equals("")) {
                                        noOverCount++;
                                    }
                                }
                            }
                        }


                        if (noOverCount > 0) {
                            CWDialog dialog = new CWDialog(LswRedoActivity.this);
                            dialog.setMessage(
                                    LswRedoActivity.this.getString(R.string.no_over_tip2))
                                    .setPositiveButton(android.R.string.cancel, null)
                                    .setNegativeButton(android.R.string.ok,
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
//                                            setResult(RESULT_OK);
                                                    dialog.dismiss();

                                                    //去掉选择题未作答的
                                                    Iterator<LswAnswer> iterator = mLswAnswerList.iterator();
                                                    while (iterator.hasNext()) {
                                                        LswAnswer l = iterator.next();
                                                        if (l.getAnswerType() == Questions.AnswerType.QUESTION_TYPE_CHOICE && !l.isAnswer()) {
                                                            Log.d("removeexist", "#############remove exist#############");
                                                            iterator.remove();
                                                        }
                                                    }


                                                    EpaperJumpManager.jumpToListenSpeakResult(R.string.listen_speak,
                                                            LswRedoActivity.this, isCorrect, downLoadInfo, mModule, checkedResource, position, mWorkId, mClassId, workTime, mServiceId, mLswAnswerList, mListenspeakExam,
                                                            RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_DO_WORK, mWorkContents, lsMode, doWorkType, effectivDate);

                                                    finish();
                                                }
                                            }).show();
                        } else {

                            EpaperJumpManager.jumpToListenSpeakResult(R.string.listen_speak,
                                    LswRedoActivity.this, isCorrect, downLoadInfo, mModule, checkedResource, position, mWorkId, mClassId, workTime, mServiceId, mLswAnswerList, mListenspeakExam,
                                    RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_DO_WORK, mWorkContents, lsMode, doWorkType, effectivDate);

                            finish();

                        }

                    }
                }
            });
        }


        setGoBackListener(new GoBackListener() {
            @Override
            public void goBack() {
                dealBack();
            }
        });

    }

    class MyThread extends Thread{
        @Override
        public void run() {
            if (isCorrect) {
                CWSys.setSharedString(ListenSpeakUtil.getUuidSaveKey(downLoadInfo, mModule, position, mWorkId, doWorkType), null);
            }
            if (EConstants.IS_YOUKE) {
                EConstants.LISTENSPEAK_IS_YOUKE_LOGIN = true;
                NewLoginDialog newLoginDialog = new NewLoginDialog(LswRedoActivity.this);
                newLoginDialog.showLoginDialog();
            } else {
                int noOverCount = 0;// 未做数

                if (null != isDoRecords && isDoRecords.size() > 0) {
                    for (ResourceDetail isDone : isDoRecords) {
                        if (!isDone.isDo()) {
                            noOverCount++;
                        }
                    }
                }

                for (LswAnswer lswAnswer : mLswAnswerList) {

                    if (lswAnswer.getAnswerType() == Questions.AnswerType.QUESTION_TYPE_FILL_BLANK || lswAnswer.getAnswerType() == Questions.AnswerType.QUESTION_TYPE_CHOICE) {
                        for (AnswerContent answerContent : lswAnswer.getAnswerContents()) {
                            if (answerContent.getContent().equals("")) {
                                noOverCount++;
                            }
                        }
                    }
                }

                if (noOverCount > 0) {

                    //去掉选择题未作答的
                    Iterator<LswAnswer> iterator = mLswAnswerList.iterator();
                    while (iterator.hasNext()) {
                        LswAnswer l = iterator.next();
                        if (l.getAnswerType() == Questions.AnswerType.QUESTION_TYPE_CHOICE && !l.isAnswer()) {
                            Log.d("removeexist", "#############remove exist#############");
                            iterator.remove();
                        }
                    }

                    EpaperJumpManager.jumpToListenSpeakResult(R.string.listen_speak,
                            LswRedoActivity.this, isCorrect, downLoadInfo, mModule, checkedResource, position, mWorkId, mClassId, workTime, mServiceId, mLswAnswerList, mListenspeakExam,
                            RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_DO_WORK, mWorkContents, lsMode, doWorkType, effectivDate);

                    finish();
                } else {
                    EpaperJumpManager.jumpToListenSpeakResult(R.string.listen_speak,
                            LswRedoActivity.this, isCorrect, downLoadInfo, mModule, checkedResource, position, mWorkId, mClassId, workTime, mServiceId, mLswAnswerList, mListenspeakExam,
                            RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_DO_WORK, mWorkContents, lsMode, doWorkType, effectivDate);

                    finish();
                }
            }
            if (null != mHandler){
                mHandler.removeCallbacks(myThread);
            }
        }
    }

    @Override
    public void onBackPressed() {
        dealBack();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void onEventMainThread(SubmitEvent submitEvent) {

        CWLog.d("SUBMIT", "我在EVENT。BUFS中" + submitEvent.getJson());
        final String id = submitEvent.getId();
        final String callbackId = submitEvent.getCallbackId();
        if ("GetJsonData".equals(id)) {

            Log.d(TAG, "###########GetJsonData json##########");
            String arg = submitEvent.getJson();
            try {
                JSONObject jsonObject = new JSONObject(arg);
                if (jsonObject.has("source")) {
                    int source = jsonObject.getInt("source");
                    if (source == ActivityPlugin.SOURCE_TYPE_LSW) {
                        //请求资源
                        lookDetail(callbackId);
                    }
                }
            } catch (Exception e) {
                e.getStackTrace();
            }

        } else if ("getDoWorkInfo".equals(id)) {
            //获取用户答案
            Log.d(TAG, "###########getDoWorkInfo json##########");
            getUserAnswer(callbackId);

        } else if ("checkResouresIds".equals(id)) {
            //取选题信息


        } else if ("reformQuestion".equals(id)) {
            if (mWorkContents != null && mWorkContents.getExamMode() != 0 && doWorkType != 1) {
                //考试模式，不容许重做
                ToastUtil.INSTANCE.toastCenter(LswRedoActivity.this, "考试模式，不容许重做题目", Toast.LENGTH_LONG, -1);
                return;
            }
            String arg = submitEvent.getJson();
            try {
                JSONObject jsonObject = new JSONObject(arg);
                if (jsonObject.has("source") && jsonObject.has("index")) {
                    int source = jsonObject.getInt("source");
                    int index = jsonObject.getInt("index");
                    if (source == ActivityPlugin.SOURCE_TYPE_LSW) {
                        //重做某题目
                        Log.d(TAG, "#########重做某题目##########" + index);
                        mCallBackId = callbackId;
                        //CWSys.setSharedString(ListenSpeakUtil.getUuidSaveKey(downLoadInfo, mModule, position, mWorkId, doWorkType), null);
                        EpaperJumpManager.jumpToListenSpeakWorkFromLsRedo(
                                R.string.go_back, LswRedoActivity.this, mWorkId,
                                downLoadInfo, mModule, position, mClassId == null ? null : mClassId, null,
                                mWorkContents == null ? null : mWorkContents.getCheckedResource(), ListenSpeakUtil.LsMode.RETRY_MODE,
                                mServiceId, mWorkContents == null ? null : mWorkContents,
                                index, mLswAnswerList, doWorkType, isCorrect);

                    }

                }
            } catch (Exception e) {
                e.getStackTrace();
            }
        }

    }

    public void onEventMainThread(LsRedoAnswersEvent lsRedoAnswersEvent) {
        if (lsRedoAnswersEvent != null && lsRedoAnswersEvent.getLswAnswers() != null) {
            ArrayList<LswAnswer> lswAnswerArrayList = lsRedoAnswersEvent.getLswAnswers();
            isDoRecords = lsRedoAnswersEvent.getIsDoRecords();
            workTime = lsRedoAnswersEvent.getWorkTime();
            if (null != lswAnswerArrayList && lswAnswerArrayList.size() > 0) {
                Log.d(TAG, "######lswAnswerArrayList.size()########" + lswAnswerArrayList.size());
                mLswAnswerList = lswAnswerArrayList;
                getUserAnswer(mCallBackId);
            } else {
                FeedbackUtil.getInstance().addFeedbackLog(FeedbackUtil.FEEDBACK_TYPE_LISTEN_SPEAK_WORK, "返回的lswAnswerArrayList为null，资源名:" + (downLoadInfo.getResourceName()), "重做页面");
            }
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

            /*if (requestCode == EpaperConstant.REQUEST_CODE_LSW_REDO_QUESTION) {
                if (data != null) {
                    isDoRecords = (ArrayList<Boolean>) getIntent().getSerializableExtra(IntentFlag.INTENT_FLAG_LSW_IS_DO_REDORD);

                    ArrayList<LswAnswer> lswAnswerArrayList = (ArrayList<LswAnswer>) data.getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ_LIST);
                    if (null != lswAnswerArrayList&&lswAnswerArrayList.size()>0){
                        Log.d(TAG, "######lswAnswerArrayList.size()########" + lswAnswerArrayList.size());
                        mLswAnswerList = lswAnswerArrayList;

                        workTime = data.getIntExtra(IntentFlag.INTENT_FLAG_WORK_TIME, workTime);
                        Log.d(TAG, "######new workTime########" + workTime);

                        getUserAnswer(mCallBackId);
                    }else{
                        FeedbackUtil.getInstance().addFeedbackLog(FeedbackUtil.FEEDBACK_TYPE_LISTEN_SPEAK_WORK, "返回的lswAnswerArrayList为null，资源名:"+(downLoadInfo.getResourceName()), "重做页面");
                    }

                }
            }*/
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    private void getUserAnswer(final String callbackId) {

        try {
            LswRedoSubAnswer lswRedoSubAnswer = new LswRedoSubAnswer();
            lswRedoSubAnswer.setUserId((int) EApplication.getInstance().getUserInfoBase().getUserId());
            lswRedoSubAnswer.setUserName(EApplication.getInstance().getUserInfoBase().getRealName());

            List<String> checkedRes = new ArrayList<>();
            if (!TextUtils.isEmpty(checkedResource)) {
                // 有选题
                String[] checkedQuesIndexs = checkedResource.split(",");
                for (int i = 0; i < checkedQuesIndexs.length; i++) {
                    checkedRes.add(checkedQuesIndexs[i]);
                }
                lswRedoSubAnswer.setCheckedResource(checkedRes);
            }
            if (doWorkType == 1 && mWorkContents != null) {
                lswRedoSubAnswer.setErrorResource(mWorkContents.getErrorResource());
            }
            List<LswAnswerContent> lswAnswerContentList = new ArrayList<LswAnswerContent>();

            if (null != mLswAnswerList && mLswAnswerList.size() > 0) {
                for (LswAnswer lswAnswer : mLswAnswerList) {
                    List<AnswerContent> answerContentList = lswAnswer.getAnswerContents();
                    List<LswAnswerSubContent> lswAnswerSubContentList = new ArrayList<LswAnswerSubContent>();
                    LswAnswerContent lswAnswerContent = new LswAnswerContent();
                    lswAnswerContent.setVersionId(lswAnswer.getVersionId());
                    if (null != answerContentList && answerContentList.size() > 0) {
                        for (AnswerContent answerContent : answerContentList) {
                            LswAnswerSubContent lswAnswerSubContent = new LswAnswerSubContent();
                            lswAnswerSubContent.setSid(answerContent.getSid());
                            lswAnswerSubContent.setContent(answerContent.getContent());
                            lswAnswerSubContent.setAnswerType(lswAnswer.getAnswerType());

                            lswAnswerSubContentList.add(lswAnswerSubContent);
                        }
                        lswAnswerContent.setAnswers(lswAnswerSubContentList);
                    }
                    lswAnswerContentList.add(lswAnswerContent);
                }
//            lswRedoSubAnswer.setVersionId();
                lswRedoSubAnswer.setWorkAnswers(lswAnswerContentList);
            }


//        lswRedoAnswer.setData(lswRedoSubAnswer);
            String json = new Gson().toJson(lswRedoSubAnswer, LswRedoSubAnswer.class);
            Log.d(TAG, "###########getUserAnswer json##########" + json);
            sendCallBack(callbackId, json);

        } catch (RuntimeException e) {
            FeedbackUtil.getInstance().addFeedbackLog(FeedbackUtil.FEEDBACK_TYPE_LISTEN_SPEAK_WORK, "回馈h5出现异常:" + e.getMessage(), "重做页面");
        }

    }


    /**
     * 查看详情
     */
    private void lookDetail(final String callbackId) {
        String mainPath = ESystem.getPackagesMainJsonPath(
                downLoadInfo.getBookId(), downLoadInfo.getChapterId());

        String resFile = ESystem.getH5PackagesJsonPathNew(downLoadInfo.getBookId(), downLoadInfo.getChapterId(), downLoadInfo.getModuleId(), downLoadInfo.getVersionId());

        if (downLoadInfo.getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_NEW) {

            getResult(resFile, callbackId);

        } else {

            EpaperDao.getInstance().getSerializableObjects(mainPath, Main.class,
                    new BaseExtCallBack(mContext, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
                        @Override
                        public void failed(Object data) {
                            showToastError((int) data);
                        }

                        @Override
                        public void failed(int errorCode, Object data) {
                            super.failed(errorCode, data);
                        }

                        @Override
                        public void success(Object data) {
                            if (isFinishing()) {
                                return;
                            }
                            Main main = (Main) data;
                            getCatalogueFile(main.getCatalogueFile(), callbackId);
                        }
                    });

        }
    }

    /**
     * 获取Module
     *
     * @param modules
     * @return
     */
    private ModuleContent getModuleByModuleId(List<Module> modules, String callbackId) {

        List<ModuleContent> moduleResourceList = new ArrayList<>();
        moduleResourceList.add(mModule.getResourceList().get(position));

//        mModule.setResourceList(moduleResourceList);

        for (Module module : modules) {
            if (module.getModuleInfo().getModuleId() == mModule.getModuleInfo()
                    .getModuleId()) {

                List<ModuleContent> resourceList = module.getResourceList();
                List<ModuleContent> resourceList1 = moduleResourceList;
                for (ModuleContent moduleContent : resourceList) {
                    //传进来的modleContent只是一个   不能传多个进来  做匹配作用
                    //最终使用的是资源里面的
                    int indexOf = resourceList1.indexOf(moduleContent);
                    if (indexOf >= 0) {
                        return moduleContent;
                    }
                }

            }
        }
        return null;
    }

    private void getCatalogueFile(String path, final String callbackId) {
        path = ESystem.getPackagesPath(LswRedoActivity.this) + File.separator + path;
        EpaperDao.getInstance().getSerializableObjects(path,
                new TypeToken<List<Module>>() {
                }.getType(), new BaseExtCallBack(mContext, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
                    @Override
                    public void failed(Object data) {
                        showToastError((int) data);
                    }

                    @Override
                    public void failed(int errorCode, Object data) {
                        super.failed(errorCode, data);
                    }

                    @Override
                    public void success(Object data) {
                        if (isFinishing()) {
                            return;
                        }
                        List<Module> modules = (List<Module>) data;

                        ModuleContent moduleContent = getModuleByModuleId(modules, callbackId);

                        if (moduleContent != null) {
                            //资源JSON数据
                            String resFile = moduleContent.getResourceFile();
                            String guid = resFile.substring(
                                    resFile.lastIndexOf("/") + 1,
                                    resFile.lastIndexOf("_"));
                            //这里用答案的
                            resFile = resFile.replace(guid, "e9430760-9f2e-4256-af76-3bd8980a9de4");

                            getResult(ESystem.getPackagesJsonPath(resFile), callbackId);

                        }
                    }
                });
    }


    /**
     * 获取网页展示内容
     *
     * @param path
     */
    private void getResult(final String path, final String callbackId) {
        EpaperDao.getInstance().getResource(path, new BaseExtCallBack(mContext, EApplication.getInstance().getUserInfoBase().getUserId() + "") {


            @Override
            public void success(final Object data) {
                if (isFinishing()) {
                    return;
                }
                //直接String
                Log.d(TAG, "###########GetJsonData sendCallBack##########" + data.toString());
                sendCallBack(callbackId, data.toString());
            }

            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);
            }

            @Override
            public void failed(Object data) {
                showToastError((int) data);
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
//        EventBus.getDefault().register(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
//        EventBus.getDefault().unregister(this);
    }


    /**
     * 返回
     */
    private void dealBack() {
        showConfirmEndDialog();
        //订正错题到结果页清除作业记录
        if (isCorrect) {
            CWSys.setSharedString(ListenSpeakUtil.getUuidSaveKey(downLoadInfo, mModule, position, mWorkId, doWorkType), null);
        }
    }

    /**
     * 退出提示框
     */
    private void showConfirmEndDialog() {
        if (isDestroy) {
            return;
        }
        CWDialog dialog = new CWDialog(this);
        dialog.setMessage(R.string.confirm_back_work);
        dialog.setPositiveButton(android.R.string.cancel, null)
                .setNegativeButton(R.string.ok,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                dialog.dismiss();
                                // 区分是否是手动停止语音播放
//                                finish 1
                                finish();
                            }
                        })
                .show();
    }
}
